<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectUser"
        >添加用户
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAuthUserAll"
        >批量取消授权
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="账号" prop="account" :show-overflow-tooltip="true"/>
      <el-table-column label="用户昵称" prop="nickname" :show-overflow-tooltip="true"/>
      <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true"/>
      <el-table-column label="手机" prop="mobile" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="isDelete">
        <!--        <template slot-scope="scope">-->
        <!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="regdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regdate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthUser(scope.row)"
          >取消授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->
    <select-user ref="select" :roleId="queryParams.roleId" @ok="handleQuery"/>
  </div>
</template>

<script>
  // import { allocatedUserList, authUserCancel, authUserCancelAll } from "@/api/system/role";
  // import selectUser from "./selectUser";
  import { getUserByRoleId, authUserCancel } from '@/api/admin/permission/RoleManage'
  import selectUser from '@/views/permission_manage/selectUser'
  import { delUser } from '@/api/admin/permission/userManager'

  export default {
    name: 'AuthUser',
    components: { selectUser },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中用户组
        userIds: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        userList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roleId: undefined,
          nickname: undefined,
          mobile: undefined
        }
      }
    },
    created() {
      const roleId = this.$route.params && this.$route.params.roleId
      if (roleId) {
        this.queryParams.roleId = roleId
        this.getList()
      }
    },
    methods: {
      /** 查询授权用户列表 */
      getList() {
        this.loading = true
        const reqData = {
          roleId: parseInt(this.queryParams.roleId),
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
          nickname: this.queryParams.nickname,
          mobile: this.queryParams.mobile
        }
        getUserByRoleId(reqData).then(response => {
            this.userList = response.data.userInfo
            this.total = response.data.total
            this.loading = false
          }
        )
      },
      // 返回按钮
      handleClose() {
        // this.$store.dispatch('tagsView/delView', this.$route)
        // this.$router.push({ path: '/system/role' })
        this.$router.push({ path: '/permissionManager/role' })
        // this.$router.replace({path:'/permissionManager/role'})
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.nickname=undefined
        this.queryParams.mobile=undefined
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.userIds = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      /** 打开授权用户表弹窗 */
      openSelectUser() {
        this.$refs.select.show()
      },
      /** 取消授权按钮操作 */
      cancelAuthUser(row) {
        const roleId = this.queryParams.roleId
        const flag = confirm('确认要取消该用户"' + row.nickname + '"角色吗？')
        if (!flag) {
          return
        }
        const reqData = { roleId: Number(roleId), userIds: [row.id] }
        authUserCancel(reqData).then(res => {
          this.getList()
          this.$message.success('取消授权成功')
        }).catch(err => {
          console.info(err)
        })
      },
      /** 批量取消授权按钮操作 */
      cancelAuthUserAll(row) {
        const roleId = this.queryParams.roleId
        // const userIds = this.userIds.join(',')
        console.info(this.userIds)
        const reqData = { roleId: Number(roleId), userIds: this.userIds }
        authUserCancel(reqData).then(res => {
          this.getList()
          this.$message.success('批量取消授权成功')
        }).catch(err => {
          console.info(err)
        })
      }
    }
  }
</script>
